//
// GPSMapEdit
// (c) Konstantin Galichsky (kg@geopainting.com), 2004-2007
//
// Object properties dialog - 'Routing' page.
//

# include "StdAfx.h"
# include "Resource.h"
# include "RgnProperties.h"
# include "Map.h"
# include "MapView.h"
# include "SetRgnExtra.h"
# include "NavitelTypes.h"
# include "Settings.h"

const size_t c_cRouteClasses = 5;
static const char * g_strRouteClass [c_cRouteClasses] = {
	"(0) Residential street/Alley/Unpaved road/Trail",
	"(1) Roundabout/Collector",
	"(2) Arterial street/Other HW",
	"(3) Principal HW",
	"(4) Major HW/Ramp"
};

const size_t c_cSpeedLimits = 8;
static const char * g_strSpeedLimit [c_cSpeedLimits] = {
	"(0) 3 MPH / 5 KPH",
	"(1) 15 MPH / 20 KPH",
	"(2) 25 MPH / 40 KPH",
	"(3) 35 MPH / 60 KPH",
	"(4) 50 MPH / 80 KPH",
	"(5) 60 MPH / 90 KPH",
	"(6) 70 MPH / 110 KPH",
	"(7) No speed limit"
};

bool CRgnRoutingPage::OnInit (HWND _hwndFocus) {
	// Speed limit.
	const HWND hSpeedLimit = GetDlgItem (IDC_SPEED_LIMIT);
	for (size_t cSpeedLimit = 0; cSpeedLimit < c_cSpeedLimits; ++ cSpeedLimit) {
		::SendMessage (hSpeedLimit, CB_INSERTSTRING, cSpeedLimit, (LPARAM) g_strSpeedLimit [cSpeedLimit]);
		::SendMessage (hSpeedLimit, CB_SETITEMDATA,  cSpeedLimit, cSpeedLimit);
		if (m_rgn.pExtra->fSpeedLimit == cSpeedLimit)
			::SendMessage (hSpeedLimit, CB_SETCURSEL, cSpeedLimit, 0);
	}

	// Route class.
	const HWND hRouteClass = GetDlgItem (IDC_ROUTE_CLASS);
	for (size_t cRouteClass = 0; cRouteClass < c_cRouteClasses; ++ cRouteClass) {
		::SendMessage (hRouteClass, CB_INSERTSTRING, cRouteClass, (LPARAM) g_strRouteClass [cRouteClass]);
		::SendMessage (hRouteClass, CB_SETITEMDATA,  cRouteClass, cRouteClass);
		if (m_rgn.pExtra->fRouteClass == cRouteClass)
			::SendMessage (hRouteClass, CB_SETCURSEL, cRouteClass, 0);
	}

	// RoadID.
	if (m_rgn.pExtra->dwRoadID != -1)
		SetDlgItemUInt (IDC_ROADID, m_rgn.pExtra->dwRoadID);
	else
		SetDlgItemText (IDC_ROADID, "-");

	// Restrictions.
	CheckDlgButton (IDC_ONE_WAY,       m_rgn.pExtra->bOneWay);
	CheckDlgButton (IDC_ROUTE_IS_TOLL, m_rgn.pExtra->bToll);
	CheckDlgButton (IDC_NO_THROUGH,    m_rgn.pExtra->bNoThrough);
	CheckDlgButton (IDC_NO_EMERGENCY,  m_rgn.pExtra->bNoEmergency);
	CheckDlgButton (IDC_NO_DELIVERY,   m_rgn.pExtra->bNoDelivery);
	CheckDlgButton (IDC_NO_CAR,        m_rgn.pExtra->bNoCar);
	CheckDlgButton (IDC_NO_BUS,        m_rgn.pExtra->bNoBus);
	CheckDlgButton (IDC_NO_TAXI,       m_rgn.pExtra->bNoTaxi);
	CheckDlgButton (IDC_NO_PEDESTRIAN, m_rgn.pExtra->bNoPedestrian);
	CheckDlgButton (IDC_NO_BICYCLE,    m_rgn.pExtra->bNoBicycle);
	CheckDlgButton (IDC_NO_TRUCK,      m_rgn.pExtra->bNoTruck);

	EnableDlgItem (IDC_NO_THROUGH, g_map.pTypeSet == & g_tsNavitel);

	return true;
}

void CRgnRoutingPage::OnCommand (WORD _wCmd, WORD _wNotifyCode, HWND _hwndCtl) {
	switch (_wCmd) {
		case IDC_SPEED_TYPE:
		case IDC_ROUTE_CLASS:
			if (_wNotifyCode == CBN_SELCHANGE)
				PropSheetChanged ();
			break;

		case IDC_ONE_WAY:
		case IDC_ROUTE_IS_TOLL:
		case IDC_NO_THROUGH:
		case IDC_NO_EMERGENCY:
		case IDC_NO_DELIVERY:
		case IDC_NO_CAR:
		case IDC_NO_BUS:
		case IDC_NO_TAXI:
		case IDC_NO_PEDESTRIAN:
		case IDC_NO_BICYCLE:
		case IDC_NO_TRUCK:
			PropSheetChanged ();
			break;
	}
}

void CRgnRoutingPage::OnApply (const PSHNOTIFY *) {
	rgn_extra_t extra = * m_rgn.pExtra;

	bool bRefreshRoadClasses = false;

	// Speed type.
	const HWND hSpeedLimit = GetDlgItem (IDC_SPEED_LIMIT);
	const size_t cSpeedLimitSel = ::SendMessage (hSpeedLimit, CB_GETCURSEL, 0, 0);
	if (cSpeedLimitSel != CB_ERR) {
		extra.fSpeedLimit = ::SendMessage (hSpeedLimit, CB_GETITEMDATA, cSpeedLimitSel, 0);
		if (extra.fSpeedLimit != m_rgn.pExtra->fSpeedLimit)
			bRefreshRoadClasses = true;
	}

	// Route class.
	const HWND hRouteClass = GetDlgItem (IDC_ROUTE_CLASS);
	const size_t cRouteClassSel = ::SendMessage (hRouteClass, CB_GETCURSEL, 0, 0);
	if (cRouteClassSel != CB_ERR) {
		extra.fRouteClass = ::SendMessage (hRouteClass, CB_GETITEMDATA, cRouteClassSel, 0);
		if (extra.fRouteClass != m_rgn.pExtra->fRouteClass)
			bRefreshRoadClasses = true;
	}

	// Restrictions.
	const DWORD dwRouteParamsOld = extra.dwRouteParams;
	extra.bOneWay       = IsDlgButtonChecked (IDC_ONE_WAY);
	extra.bToll         = IsDlgButtonChecked (IDC_ROUTE_IS_TOLL);
	extra.bNoThrough    = IsDlgButtonChecked (IDC_NO_THROUGH);
	extra.bNoEmergency  = IsDlgButtonChecked (IDC_NO_EMERGENCY);
	extra.bNoDelivery   = IsDlgButtonChecked (IDC_NO_DELIVERY);
	extra.bNoCar        = IsDlgButtonChecked (IDC_NO_CAR);
	extra.bNoBus        = IsDlgButtonChecked (IDC_NO_BUS);
	extra.bNoTaxi       = IsDlgButtonChecked (IDC_NO_TAXI);
	extra.bNoPedestrian = IsDlgButtonChecked (IDC_NO_PEDESTRIAN);
	extra.bNoBicycle    = IsDlgButtonChecked (IDC_NO_BICYCLE);
	extra.bNoTruck      = IsDlgButtonChecked (IDC_NO_TRUCK);
	if (dwRouteParamsOld != extra.dwRouteParams)
		bRefreshRoadClasses = true;

	SetObjectRouteParams (m_rgn, extra.dwRouteParams);
		
	if (g_bShowRoadClasses && bRefreshRoadClasses)
		RefreshRequired ();
}